package com.xingkeduo.pojo.utils;

import com.xingkeduo.util.tool.ReturnUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;

/**
 * @author chi  2019-01-24 17:23
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class ResponseUtils<T> {

    /**
     * 存储错误信息
     */
    private String msg = "success";

    private String path;

    private Long timestamp = Instant.now().toEpochMilli();

    private T data;

    private String code;

    private int status;
    
    private Integer reviews;


    public static <T> ResponseUtils getSuccess(T data) {
        ResponseUtils<Object> result = ResponseUtils.builder()
                .msg("success")
                .data(data)
                .timestamp(Instant.now().toEpochMilli())
                .status(ReturnUtil.Status.SUCCESS.getIndex())
                .build();
        return result;
    }

    /**c拉c 需要code
     * @param data:
     * @return : com.xingkeduo.pojo.utils.ResponseUtils
     * @author : gaozifeng
     * @date : 2019/7/16 16:02
     */
    public static <T> ResponseUtils getSuccess(T data,String code) {
        ResponseUtils<Object> result = ResponseUtils.builder()
                .msg("success")
                .data(data)
                .code(code)
                .timestamp(Instant.now().toEpochMilli())
                .status(ReturnUtil.Status.SUCCESS.getIndex())
                .build();
        return result;
    }

    public static ResponseUtils getSuccess() {
        return getSuccess(null);
    }

    /**c拉c 需要code
     * @param :
     * @return : com.xingkeduo.pojo.utils.ResponseUtils
     * @author : gaozifeng
     * @date : 2019/7/16 16:03
     */
    public static ResponseUtils getSuccess(String code) {
        return getSuccess(null,code);
    }


    /**
     * 错误信息
     *
     * @param msg
     * @param code
     * @return
     */
    public static ResponseUtils error(String msg, String code) {
        ResponseUtils<Object> result = ResponseUtils.builder()
                .msg(msg)
                .timestamp(Instant.now().toEpochMilli())
                .code(code)
                .status(ReturnUtil.Status.FAIL.getIndex())
                .build();
        return result;
    }

    /**
     * 错误信息
     *
     * @return
     */
    public static ResponseUtils error() {
        ResponseUtils<Object> result = ResponseUtils.builder()
                .msg("请求失败")
                .timestamp(Instant.now().toEpochMilli())
                .code("-1")
                .status(ReturnUtil.Status.FAIL.getIndex())
                .build();
        return result;
    }

    public boolean success(){
        return status == 1;
    }
}

